package com.czk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.czk.model.hosp.Department;
import com.czk.repository.DepartmentRepository;
import com.czk.service.DepartmentService;
import com.czk.vo.hosp.DepartmentQueryVo;
import com.czk.vo.hosp.DepartmentVo;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/11 21:03
 */
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Resource
    private DepartmentRepository departmentRepository;

    @Override
    public void save(Map<String, Object> map) {
        // 将json转为对象
        Department department = JSONObject.parseObject(JSONObject.toJSONString(map), Department.class);
        Department departmentExsit = departmentRepository.getDepartmentByHoscodeAndDepcode(department.getHoscode(), department.getDepcode());
        if (departmentExsit != null) {
            departmentExsit.setUpdateTime(new Date());
            departmentExsit.setIsDeleted(0);
            // 更新
            departmentRepository.save(departmentExsit);
        } else {
            department.setCreateTime(new Date());
            department.setUpdateTime(new Date());
            department.setIsDeleted(0);
            // 添加操作
            departmentRepository.save(department);
        }

    }

    @Override
    public Page<Department> findPageDepartment(int page, int limit, DepartmentQueryVo departmentQueryVo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentQueryVo, department);
        // 设置分页对象
        PageRequest pageAble = PageRequest.of(page - 1, limit);
        // 设置条件对象
        ExampleMatcher exampleMatcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);
        Example<Department> example = Example.of(department, exampleMatcher);
        Page<Department> departmentList = departmentRepository.findAll(example, pageAble);

        return departmentList;
    }

    @Override
    public void removeDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            departmentRepository.deleteById(department.getId());
        }
    }

    @Override
    public List<DepartmentVo> findDeptTree(String hoscode) {
        List<DepartmentVo> list = new ArrayList<>();
        // 根据医院编号查
        Department departmentQuery = new Department();
        // 封装查询条件
        departmentQuery.setHoscode(hoscode);
        Example<Department> example = Example.of(departmentQuery);
        List<Department> departmentList = departmentRepository.findAll(example);
        // 分组，根据大科室编号
        Map<String, List<Department>> departmentMap = departmentList.stream().collect(Collectors.groupingBy(Department::getBigcode));
        // 遍历map
        departmentMap.forEach((key, value) -> {
            // 封装大科室
            DepartmentVo departmentVo1 = new DepartmentVo();
            departmentVo1.setDepcode(key);
            departmentVo1.setDepname(value.get(0).getBigname());
            // 封装小科室
            List<DepartmentVo> children = new ArrayList<>();
            for (Department department : value) {
                DepartmentVo departmentVo2 = new DepartmentVo();
                departmentVo2.setDepcode(department.getDepcode());
                departmentVo2.setDepname(department.getDepname());
                children.add(departmentVo2);
            }
            departmentVo1.setChildren(children);
            list.add(departmentVo1);
        });
        return list;
    }

    @Override
    public String getDepName(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        if (department != null) {
            return department.getDepname();
        }
        return null;
    }

    @Override
    public Department getDepartment(String hoscode, String depcode) {
        Department department = departmentRepository.getDepartmentByHoscodeAndDepcode(hoscode, depcode);
        return department;
    }
}
